<?php

namespace backend\controllers;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\Logs;
use backend\models\Goods;
use backend\models\User;
/**
 * LogsSearch represents the model behind the search form of `backend\models\Logs`.
 */
class LogsSearch extends Logs
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id'], 'integer'],
            [['goodsid', 'opid','oprtype', 'remark', 'oprtime'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Logs::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => ['defaultOrder' => ['id' => SORT_DESC]], // 新增配置项 默认 id 倒序
        ]);

        $this->load($params);
        if(!empty($params) && isset($params['LogsSearch'])){
            $logssearch = $params['LogsSearch'];
        }
        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        if(!empty($params['gid'])){
            $query->andFilterWhere(['goodsid'=>$params['gid']]);
            $query->andFilterWhere(['<>','price_remark',''])->andWhere(['is not','price_remark',null]);
        }
        if($this->goodsid){
            $goods = new Goods();
            $goodids = $goods->find()->where(['like', 'goodsno', $this->goodsid])->select('id')->asArray()->all();
            $g=[];
            foreach($goodids as $item){
                $g[]=$item['id'];
            }
            $query->andFilterWhere(['in','goodsid',$g]);
        }

        if($this->opid){

            $query->andFilterWhere(['opid'=>$this->opid]);
        }
        if(isset($logssearch['datefrom']) && $logssearch['datefrom']){
            $query->andFilterWhere(['>','oprtime',$logssearch['datefrom']]);
        }
        if(isset($logssearch['dateto']) && $logssearch['dateto']){
            $query->andFilterWhere(['<','oprtime',$logssearch['dateto']]);
        }

        if($this->oprtype === '0'  ||  !empty($this->oprtype) ){

            $oprtypes=[
                '登录',
                '退出登录',
                '创建商品',
                '更新商品',
                '批量导入_创建商品',
                '批量导入_更新商品',
                '批量导出',
                '审核通过',
                '审核不通过',
                '审核报告',
                '审核通知'];
            $query->andFilterWhere(['oprtype'=>$oprtypes[$this->oprtype]]);

        }

        $query->andFilterWhere(['like', 'remark', $this->remark]);

//                $commandQuery = clone $query;
//        echo $commandQuery->createCommand()->getRawSql();
        return $dataProvider;
    }
}
